/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","fu/app/comm/comm",
    "fu/app/datatable/datatable.servers",
    "fu/app/datatable/datatable.ui"],
    function($,$bb,$comm,$datatableServers,$datatableUi){
	var servers = {};
	var URL = $bb.baseUrl("followup");

    /**
     * 入参
     */
    var inParams = function(args){
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "phoneNum": $.trim($("#tel").val()),
                "patientName": $.trim($("#username").val()),
                "examinationPaperName":"GMS"
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    };
    /**
     * columns
     */
    var weekName = function(e){
        var diff = $comm.dateDiff(e.birthday, e.testTime).days;
        return parseInt(diff/7) + "周"+ diff%7 + "天"
    };
    var pregnancDay = function(e){
        return parseInt(e.pregnancDay/7) + "周"+ e.pregnancDay%7 + "天"
    }
    var columnsData = function(){
        var columnsArr = [
            {"data":'userName'},
            {"data":'phoneNum'},
            {"data":weekName},
            {"data":pregnancDay},
            {"data":'createTime'},
            {
                "data": null,
                "className":"operate"
            }
        ];
        return columnsArr;
    }

    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        $('td.operate', row).html('<div class="btn-look">查看</div>');
        $('td.operate', row).find(".btn-look").click(function () {
            $bb.openPage("gmsDetail",{"info":data});
        });
        return row;
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        $("#gmsList").prev("div.row").remove();
        $("#username, #tel").bind("input propertychange",function(){
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
        });
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "followup/service/patient/evaluation/patientListqry?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams,
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

	return servers;
});